package com.mjlf.test_kafka.send;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mjlf.test_kafka.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * @ClassName: KafkaSender
 * @Author: majun
 * @CreateDate: 2018/10/30 11:04
 * @Version: 1.0
 * @Description: TODO
 */

@Component
public class KafkaSender {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    private Gson gson = new GsonBuilder().create();

    public void send() {
        Message message = new Message();
        message.setId(System.currentTimeMillis());
        message.setMsg(UUID.randomUUID().toString());
        message.setSendTime(new Date());
        kafkaTemplate.send("test", gson.toJson(message));
    }

}
